package com.group.three.untils;



import cn.hutool.json.JSONException;
import cn.hutool.jwt.JWT;
import com.group.three.entity.dto.UserPwdLoginInfoDto;

import java.nio.charset.StandardCharsets;

public class JwtUtils {
    //jwt私钥，需要定期更新
    public static final byte[] secretKey = "kgc-jwt-token".getBytes(StandardCharsets.UTF_8);
    //将用户名和id加密生成jwt
    public static String encodeToJwt(UserPwdLoginInfoDto userInfo){
        return JWT.create()
                .setHeader("head", "啊啊啊啊大便啊")
                .setPayload("userInfo", userInfo)
                .setKey(secretKey)
                .sign();
    }

    //jwt验证
    public static boolean verify(String jwtToken){
        try{
            boolean verify = JWT.of(jwtToken).setKey(secretKey).verify();
        }catch (JSONException exception){
            return false;
        }

        return true;
    }

    //将jwt字符串解密获取相关信息
    public static JWT decodeFromJwt(String jwtToken){
        return JWT.of(jwtToken).setKey(secretKey);
    }
}